What is eslint-plugin-yml?
eslint-plugin-yml is an ESLint plugin that provides linting rules for YAML files. It helps ensure consistency and correctness in YAML files by enforcing specific coding standards and best practices.
What are eslint-plugin-yml's main functionalities?
Indentation
This rule enforces consistent indentation in YAML files. The example configuration sets the indentation to 2 spaces.
module.exports = {
'yml/indent': ['error', 2]
};
Key Sorting
This rule enforces sorted keys within YAML mappings. The example configuration sorts keys in ascending order, case-insensitively, and using natural sort order.
module.exports = {
'yml/sort-keys': ['error', 'asc', { 'caseSensitive': false, 'natural': true }]
};
No Empty Mapping Values
This rule disallows empty values in YAML mappings. The example configuration treats any empty mapping value as an error.
module.exports = {
'yml/no-empty-mapping-value': 'error'
};
Other packages similar to eslint-plugin-yml
yaml-lint
yaml-lint is a simple linter for YAML files that checks for syntax errors. Unlike eslint-plugin-yml, it does not integrate with ESLint and offers fewer customization options.
Introduction
eslint-plugin-yml is ESLint plugin provides linting rules for YAML.
:name_badge: Features
This ESLint plugin provides linting rules for YAML.
- You can use ESLint to lint YAML.
- You can find out the problem with your YAML files.
- You can apply consistent code styles to your YAML files.
- Supports Vue SFC custom blocks such as
<i18n lang="yaml">
.
Requirements vue-eslint-parser
v7.3.0 and above. - Supports ESLint directives. e.g.
# eslint-disable-next-line
- You can check your code in real-time using the ESLint editor integrations.
You can check on the Online DEMO.
:question: How is it different from other YAML plugins?
Plugins that do not use AST
e.g. eslint-plugin-yaml
These plugins use the processor to parse and return the results independently, without providing the ESLint engine with AST and source code text.
Plugins don't provide AST, so you can't use directive comments (e.g. # eslint-disable
).
Plugins don't provide source code text, so you can't use it with plugins and rules that use text (e.g. eslint-plugin-prettier, eol-last).
eslint-plugin-yml works by providing AST and source code text to ESLint.
:book: Documentation
See documents.
:cd: Installation
npm install --save-dev eslint eslint-plugin-yml
Requirements
- ESLint v6.0.0 and above
- Node.js v12.22.x, v14.17.x, v16.x and above
:book: Usage
Configuration
Use .eslintrc.*
file to configure rules. See also: https://eslint.org/docs/user-guide/configuring.
Example .eslintrc.js:
module.exports = {
extends: [
'plugin:yml/standard'
],
rules: {
}
}
This plugin provides configs:
plugin:yml/base
... Configuration to enable correct YAML parsing.plugin:yml/recommended
... Above, plus rules to prevent errors or unintended behavior.plugin:yml/standard
... Above, plus rules to enforce the common stylistic conventions.plugin:yml/prettier
... Turn off rules that may conflict with Prettier.
See the rule list to get the rules
that this plugin provides.
Parser Configuration
If you have specified a parser, you need to configure a parser for .yaml
.
For example, if you are using the "@babel/eslint-parser"
, configure it as follows:
module.exports = {
extends: ["plugin:yml/standard"],
parser: "@babel/eslint-parser",
overrides: [
{
files: ["*.yaml", "*.yml"],
parser: "yaml-eslint-parser",
},
],
};
Running ESLint from the command line
If you want to run eslint
from the command line, make sure you include the .yaml
extension using the --ext
option or a glob pattern, because ESLint targets only .js
files by default.
Examples:
eslint --ext .js,.yaml,.yml src
eslint "src/**/*.{js,yaml,yml}"
:computer: Editor Integrations
Visual Studio Code
Use the dbaeumer.vscode-eslint extension that Microsoft provides officially.
You have to configure the eslint.validate
option of the extension to check .yaml
files, because the extension targets only *.js
or *.jsx
files by default.
Example .vscode/settings.json:
{
"eslint.validate": [
"javascript",
"javascriptreact",
"yaml"
]
}
:white_check_mark: Rules
The --fix
option on the command line automatically fixes problems reported by rules which have a wrench :wrench: below.
The rules with the following star :star: are included in the config.
YAML Rules
Extension Rules
:rocket: To Do More Verification
Verify using JSON Schema
You can verify using JSON Schema by checking and installing eslint-plugin-json-schema-validator.
Verify the Vue I18n message resource files
You can verify the message files by checking and installing @intlify/eslint-plugin-vue-i18n.
:beers: Contributing
Welcome contributing!
Please use GitHub's Issues/PRs.
Development Tools
npm test
runs tests and measures coverage.npm run update
runs in order to update readme and recommended configuration.
Working With Rules
This plugin uses yaml-eslint-parser for the parser. Check here to find out about AST.
:couple: Related Packages
:lock: License
See the LICENSE file for license rights and limitations (MIT).